<?php if(!$_GET["id"]) {header("location: product.php");} ?>
<?php include ('head.php'); ?>
<script type="text/javascript">
	window.productId = "<?=$_GET['id'];?>";
	var $golnum = 3;
	// 微博登陆
	
	var wbLogin = function(o,t) { 
		if (typeof $(o) == 'undefined'||$(o)=='') {
			checkcode('2_login');
		}else{ 
			if ($(o).hasClass('logins-off')) {
				return false;
			}else if ($(o).hasClass('share-off')){
				return false;
			}else if ($(o).hasClass('logins')){
				var $checknum = 78; 
				checkcode((parseInt(t)+$checknum)+'_login_share_'+t);
			}else if ($(o).hasClass('share')){
				var $checknum = 64;
				checkcode((parseInt(t)+$checknum)+'_share_'+t);
			}
		}
		var wbLoginUrl = '/app/index.php?c=weibo&m=login'; 
		$.oauthpopup({path: wbLoginUrl});
	}

	var wbLogout = function() {
		checkcode('1_logout'); 
		var wbLogoutUrl = '/app/index.php?c=weibo&m=logout'; 
		$.oauthpopup({path: wbLogoutUrl});
	}

	window.wbAuthSuccess = function(authWindow) {
		console.log('wbAuthSuccess');
		if (app.preProductShare) {
			var $checknum = 64;
			var t = $('#product-time-left').attr('data-rel');
			checkcode((parseInt(t)+$checknum)+'_share_'+t);
			var post = app.wbShareProduct(productId, app.preProductShare);
			// authWindow.close();
			if (post.error) {
				showpop('popproduct3','您分享的太快了,请变更内容再试.');
				return false;//alert('您分享的太快了,请变更内容再试.');
			}
			showpop('popproduct3','分享成功!<br/><span style="font-size: 18px;display: block;clear: both;">请记得上午10点来抢购哦!</span>');
			$('#popproduct3 .mm_close').click(function(){
				app.redirect('http://shop.mediamarkt.cn/');
			});
		}
	}


	$(function() {
		
		var _ajax = function (url, submit, method) {
			var result = {};
			submit = submit || {};
			$.ajax({
				url:		url,
				data:		submit,
				type:		method,
				dataType:	'json',
				async:		false,
				cache:		false,
				complete:	function(xml) {},
				success:	function(str) {
					result = str
				}
			})
			return typeof result['error'] == 'undefined' ? result : false;
		}

		var _get = function(url, submit) {
			return _ajax(url, submit, 'GET');
		}

		var _post = function(url, submit) {
			return _ajax(url, submit, 'POST');
		}

		// 获取用户信息
		var getUserInfo = function() {
			return _get('/app/index.php?d=api&c=user_controller&m=get_user_info');
		}

		// 获取产品信息
		var getProduct = function(id) {
			var data = _get('/app/index.php?d=api&c=product&m=get_product&product_id='+id);
			return data ? data.product : false;
		}

		// 微博分享产品
		var wbShareProduct = function(id, text) {
			return _post('/app/index.php?d=api&c=weibo&m=share_product', 
				{product_id: id, text: text});
		}

		
		var updateUserState = function() {
			var user = getUserInfo();
			// 用户已登陆
			var $class = 'logins-off';
			var $classo= 'logins';
			$('#user-login-info').hide();
			if (user) {
				// 激活分享按钮 
				$class = 'share-off';
				$classo= 'share';
				// 欢迎用户信息
				$('#user-login-info').show();
				$('#user-wb-screen-name').html(user.user_wb_screen_name);
			}
			$('#wb-share-button').addClass($class);
			$('#agree').click(function() {
                if (!!$("#agree").attr("checked")) {
                    $('#wb-share-button').removeClass($class).addClass($classo);
                }else{
	                $('#wb-share-button').removeClass($classo).addClass($class);
                } 
            });
           
		}


		var updateProduct = function() {
			var product = getProduct(window.productId);
			if(!product || product.state != 'in_vote') {
				app.redirect('product.php');
			}
			app.product = product;
			//showpop('popproduct1');
			$('#product-discount').html(product['product_high_discount']*10+"<span>%</span>");  // 产品折扣
			$('#product-sharing-picture').attr('src', product['product_sharing_picture']);  // 产品分享图片
			$('#product-vote-count').html(product['product_vote_count']);  // 产品投票数
			$('#product-share-count').html(product['product_share_count']);  // 产品分享数
			var $date = new Date();
			$('#product-time-left').attr('data-datetime',$date.getFullYear()+"/"+($date.getMonth()+1)+"/"+$date.getDate()+" "+MillisecondToDate(product['time_left']));
			//$('#wb-share-button').click(function(){
	        //    wbLogin($('#wb-share-button'),product['product_order']);
            //}); 
            var $checkcode = parseInt(product['product_order']); 
            var $checknum  = 92;
            $('#backtoproduct').attr('onclick','return checkcodea("'+($checkcode+$checknum)+'_back_'+$checkcode+'",this);');
            $('#product-time-left').attr('data-rel',product['product_order']);
            $('.mm_close_mian').attr('data-rel',product['product_order']);
            $("#product-discount-s").html(product['product_low_discount']*10+"<span>%</span>");
		}
		
		updateUserState();
		updateProduct(); 

		 listenWbLogAction();
		 function listenWbLogAction() {
		 	if (app.user) {
		 		$(document).delegate('[action="wb-logout"]', 'click', 
		 			function() { 
			 			var $checknum = 78;
			 			var t = $('#product-time-left').attr('data-rel');
			 			checkcode((parseInt(t)+$checknum)+'_login_share_'+t);
			 			window.app.wbLogout();
		 			}
	 			);
		 	} else {
		 		// 监听登陆
		 		$(document).delegate('[action="wb-login"]', 'click', 
		 			function() {
			 			checkcode('2_login');
			 			window.app.wbLogin()
		 			}
	 			);
	 			// 监听登陆并分享
		 		$(document).delegate('.logins[action="wb-login-and-share"]', 'click', 
		 			function() {
			 			$.oauthpopup({path: '/app/index.php?c=weibo&m=login', 
			 				callback: function() {
			 					checkcode('2_login');
						 		var $checknum = 64;
						 		var t = $('#product-time-left').attr('data-rel');
								checkcode((parseInt(t)+$checknum)+'_share_'+t);
						 		var post = app.wbShareProduct(productId, 
						 			$('#sinawb').val());
						 		if (post.error) {
						 			showpop('popproduct3','您分享的太快了,请变更内容再试.');
						 			return false;//alert('您分享的太快了,请变更内容再试.');
						 		}
						 		showpop('popproduct3','分享成功!<br/><span style="font-size: 18px;display: block;clear: both;">请记得上午10点来抢购哦!</span>');
								$('#popproduct3 .mm_close').click(function(){
									app.redirect(app.product.product_full_price_link);
								});
			 				}
			 			});
		 			}
	 			);
		 	}
		 }

		 // 监听微博分享
		 listenWbShare();
		 function listenWbShare() {
		 	if (!app.user) return;
		 	$(document).delegate('#wb-share-button.share', 'click', function() {
		 		var $checknum = 64;
		 		var t = $('#product-time-left').attr('data-rel');
				checkcode((parseInt(t)+$checknum)+'_share_'+t);
		 		var post = app.wbShareProduct(productId, $('#sinawb').val());
		 		if (post.error) {
		 			showpop('popproduct3','您分享的太快了,请变更内容再试.');
		 			return false;//alert('您分享的太快了,请变更内容再试.');
		 		}
		 		showpop('popproduct3','分享成功!<br/><span style="font-size: 18px;display: block;clear: both;">请记得上午10点来抢购哦!</span>');
				$('#popproduct3 .mm_close').click(function(){
					app.redirect(app.product.product_full_price_link);
				});
	         });
	     }

	     // 微博分享按钮样式
		 updateWbShareButton();
		 function updateWbShareButton() {
			var $class = app.user ? 'share' : 'logins';
		 	var $classo= app.user ? 'share-off' : 'logins-off';
		 	$('#wb-share-button').removeClass('logins-off').addClass($classo);
		 	$('#agree').click(function() {
                if ($("#agree").attr("checked")) {
                     $('#wb-share-button').removeClass($classo).addClass($class);
                 }else{
	                $('#wb-share-button').removeClass($class).addClass($classo);
                }
             });
		 }

	})	
</script>
<div class="mm_top">
<div class="mm_logo floatl" ></div>
<div class="mm_text floatr">
<p>想抢到心仪的宝贝？</p><p class="font16">赶快用 <a action="wb-login"><img class="sinabtn1" src="static/images/sinabtn1.png" width="192" height="30"/></a> 为它的终极折扣冲刺吧！</p><p>每天<span>10:00</span><span class="am">AM</span>开始冲刺，限时<span>24</span>小时，</p><p>想冲要快！</p>
<p class="font14">* 所有参加活动产品的配送地区仅限上海</p>
<div class="showbtn"><a href="http://shop.mediamarkt.cn" onclick="checkcode('3_shop_online');" target="_blank"></a></div>
</div>
<div class="clearfix"></div>
</div>
<div class="mm_produck">
	<div class="mm_sinawb">
		<div class="mm_productsimg">
			<div class="cover">终极折扣<br/><span id="product-discount"></span><div class="coverin">心动折扣<br/><span id="product-discount-s"></span></div></div>
			<div class="timelist">冲刺倒计时 <span id="product-time-left" data-time="true" data-ajax="backlist"></span></div>
			<div class="imglist"><img id="product-sharing-picture" src="" width="430" height="250" /></div>
		</div>
		<div class="mm_productswb">
			<div class="textlistwb">已有 <span id="product-vote-count">0</span> <img src="static/images/icon1.png" width="12" height="12" /> 为该产品的终极折扣冲刺，已被分享 <span id="product-share-count">0</span> 次</div>
			<div class="fontimg"><img src="static/images/font.png" width="374" height="100" /></div>
			<div class="sendcontent" > 
<label class="sendcontent_sinawb" for="sinawb">我正在@万得城电器 #我打我折# 活动中，为心仪产品冲刺终极折扣！http://www.mediamarkt.cn/</label>
			 <textarea id="sinawb" name="sinawb" class="textarea" onkeyup="checkWordNum(this)">今日产品太诱人了 ...</textarea> 
			</div>
			<div class="agreeandnum">
				<label class="floatl"> <input type="checkbox" id="agree" name="agree" /> 我已经阅读并同意<a href="javascript:void(0)" onclick="showrule()">活动详情与规则</a></label>
				<span class="floatr">您还可以输入 <span id="sinalengthmax">89</span> 字</span>
				<div class="clearfix"></div>
			</div>
			<div class="btns">
				<a href="product.php#center" id="backtoproduct" class="back"></a>
				<a id="wb-share-button" href="javascript:void(0)" action="wb-login-and-share" class="logins-off"></a>
				<div class="clearfix"></div>
			</div>
		</div>
	</div>
</div>
<?php include ('foot.php'); ?>